package Dao;
import Dao.MemberDao;
import java.io.*;
import entity.Member;
import org.omg.IOP.IOR;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class MemberDaoimpl implements MemberDao{

    /*
    办理会员操作
     */
    @Override
    public boolean insertMember(Member member) {
        try{
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Member.txt",true));
            bufferedWriter.write(member.getId()+","+member.getQQmember()+","+member.getVideomember()+","+member.getMusicMember()+","+member.getLookmember());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }
        catch(IOException e){
            System.out.println("io异常,办理失败!");
            return false;
        }
    }
    /*
    删除用户会员权益
     */
    @Override
    public boolean deleteMemberByVideomember(Integer id, String Videomember) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Member.txt"));
            List<Member> memberList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Member member=new Member(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                memberList.add(member);
            }
            for(int i=0;i<memberList.size();i++){
                Member member=memberList.get(i);
                if(member.getId().equals(id)){
                    if(member.getVideomember().equals(Videomember)){
                        memberList.remove(i);
                    }
                    else{
                        System.out.println("该用户里不存在该会员权益,删除失败!");
                    }
                    }
                else{
                    System.out.println("查无此id,删除失败!");
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Member.txt"));
            for(int i=0;i<memberList.size();i++){
                Member member=memberList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",member.getId(),member.getQQmember(),member.getVideomember(),member.getMusicMember(),member.getLookmember());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
                bufferedWriter.close();
            return true;
        }
        catch (IOException e){
            System.out.println("io异常,删除会员权益失败!");
            return false;
        }
    }
    /*
    根据id,查询用户的会员权益
     */
    @Override
    public List<Member> seekMemberById(Integer id) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Member.txt"));
            List<Member> memberList=new ArrayList<>();
            List<Member> memberList1=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Member member1=new Member(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                memberList.add(member1);
            }
            for(int i=0;i<memberList.size();i++){
                Member member1=memberList.get(i);
                if(member1.getId().equals(id)){
                    memberList1.add(member1);
                }
            }
            bufferedReader.close();
            return memberList1;
        }
        catch (IOException e){
            System.out.println("查询失败!");
            return null;
        }
    }
    /*
    根据用户id,更改用户会员信息
     */
    @Override
    public boolean changeMemberById(Integer id, String Videomember, Member member) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Member.txt"));
            List<Member> memberList=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Member member1=new Member(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                memberList.add(member1);
            }
            for(int i=0;i<memberList.size();i++){
                Member member1=memberList.get(i);
                if(member1.getId().equals(id)){
                    if(member1.getVideomember().equals(Videomember)){
                        memberList.set(i,member);
                    }
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Member.txt"));
            for(int i=0;i<memberList.size();i++){
                Member member1=memberList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s",member1.getId(),member1.getQQmember(),member1.getVideomember(),member1.getMusicMember(),member1.getLookmember());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }
        catch (IOException e){
            System.out.println("io异常,更改会员权益失败!");
            return false;
        }
    }
    /*
    查询所有用户的会员权益
     */
    @Override
    public List<Member> seekAllMember() {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Member.txt"));
            List<Member> memberList=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Member member=new Member(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                memberList.add(member);
            }
            bufferedReader.close();
            return memberList;
        }
        catch (IOException e){
            System.out.println("io异常,查询会员权益操作失败!");
            return null;
        }
    }

}
